<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>关于socket部分功能的使用 | 蓝湖畔淅淅沥沥的雨</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="前言 都是复制的以前写过的东西  python UDP 客户端+服务端 封装进了一个类 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import socketimport threadingfrom typing import Tupleclass SR:    de">
<meta property="og:type" content="article">
<meta property="og:title" content="关于socket部分功能的使用">
<meta property="og:url" content="http://example.com/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="蓝湖畔淅淅沥沥的雨">
<meta property="og:description" content="前言 都是复制的以前写过的东西  python UDP 客户端+服务端 封装进了一个类 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import socketimport threadingfrom typing import Tupleclass SR:    de">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-06-11T02:00:03.000Z">
<meta property="article:modified_time" content="2022-06-11T02:24:40.148Z">
<meta property="article:author" content="StarsWhisper">
<meta property="article:tag" content="socket">
<meta property="article:tag" content="python">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="蓝湖畔淅淅沥沥的雨" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/plugin/bganimation/bg.css">

  

  <link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet" type="text/css">
<meta name="generator" content="Hexo 6.1.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <div class="widget-wrap mobile-header">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

        <section id="main"><article id="post-关于socket部分功能的使用" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" class="article-date">
  <time class="post-time" datetime="2022-06-11T02:00:03.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      关于socket部分功能的使用
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">基础知识</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/socket/">socket</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a class="markdownIt-Anchor" href="#前言"></a> 前言</h2>
<p>都是复制的以前写过的东西</p>
<h2 id="python-udp-客户端服务端"><a class="markdownIt-Anchor" href="#python-udp-客户端服务端"></a> python UDP 客户端+服务端</h2>
<p>封装进了一个类</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"><span class="keyword">from</span> typing <span class="keyword">import</span> <span class="type">Tuple</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SR</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, ip = <span class="string">&#x27;0.0.0.0&#x27;</span>, port = <span class="number">27013</span>, sz = <span class="number">2048</span></span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line">        self.__socket.bind((ip, port))</span><br><span class="line">        self.__sz = sz</span><br><span class="line">        self.__lock = threading.Lock()</span><br><span class="line">        self.__datapool = []</span><br><span class="line">        </span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__t_receive</span>(<span class="params">self</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;接收消息, 应该放到单独的线程中&#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                data, address = self.__socket.recvfrom(self.__sz)</span><br><span class="line">                self.__lock.acquire()</span><br><span class="line">                data = data.decode(<span class="string">&#x27;UTF-8&#x27;</span>)</span><br><span class="line">                self.__datapool.append((data, address))</span><br><span class="line">                self.__lock.release()</span><br><span class="line">            <span class="keyword">except</span>:</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">receive</span>(<span class="params">self</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;开启消息接收的方法&#x27;&#x27;&#x27;</span></span><br><span class="line">        t = threading.Thread(target = self.__t_receive, name = <span class="string">&#x27;receiver&#x27;</span>)</span><br><span class="line">        t.start()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">send</span>(<span class="params">self, data, address</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;发送消息&#x27;&#x27;&#x27;</span></span><br><span class="line">        self.__socket.sendto(data.encode(<span class="string">&#x27;UTF-8&#x27;</span>), address)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">getData</span>(<span class="params">self</span>) -&gt; <span class="type">Tuple</span>[<span class="built_in">str</span>, <span class="type">Tuple</span>[<span class="built_in">str</span>, <span class="built_in">int</span>]]:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        从消息队列中提取一条最早的数据</span></span><br><span class="line"><span class="string">        return : (msg, (ip, port))</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="comment">#self.__lock.acquire()</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(self.__datapool) == <span class="number">0</span>: <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">        data = self.__datapool.pop(<span class="number">0</span>)</span><br><span class="line">        <span class="comment">#self.__lock.release()</span></span><br><span class="line">        <span class="keyword">return</span> data</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">inactive</span>(<span class="params">self</span>):</span><br><span class="line">        self.__socket.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="python-tcp-服务端"><a class="markdownIt-Anchor" href="#python-tcp-服务端"></a> python TCP 服务端</h2>
<p>一个摆烂的服务端脚本删减</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># from MemoSqliteFunctions import *</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line">HOST = <span class="string">&#x27;0.0.0.0&#x27;</span></span><br><span class="line">PORT = <span class="number">22222</span></span><br><span class="line">BUFF_SIZE = <span class="number">4096</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">service_run</span>():</span><br><span class="line">    <span class="comment"># 建立Socket连接, AF_INEF说明使用IPv4地址, SOCK_STREAM指明TCP协议</span></span><br><span class="line">    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">    serverSocket.bind((HOST, PORT))</span><br><span class="line">    serverSocket.listen(<span class="number">3</span>)<span class="comment"># 监听 </span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;Run at <span class="subst">&#123;HOST&#125;</span>:<span class="subst">&#123;PORT&#125;</span>&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 接收TCP连接, 并返回新的Socket对象</span></span><br><span class="line">        sk, addr = serverSocket.accept()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;客户端: <span class="subst">&#123;addr&#125;</span> 链接&quot;</span>)</span><br><span class="line"></span><br><span class="line">        task = threading.Thread(target = TCP_task, args=(sk, ), name = <span class="string">&#x27;tcp_task&#x27;</span>)</span><br><span class="line">        task.start()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        conn.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">TCP_task</span>(<span class="params">sk : socket.socket</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">	<span class="string">&#x27;&#x27;&#x27;线程任务&#x27;&#x27;&#x27;</span></span><br><span class="line">	</span><br><span class="line">    <span class="comment"># ......</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            <span class="comment"># 接收客户端发送的数据</span></span><br><span class="line">            data = sk.recv(BUFF_SIZE)</span><br><span class="line">            data = data.decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">            <span class="comment"># ......</span></span><br><span class="line">		<span class="keyword">except</span>:</span><br><span class="line">            <span class="comment"># ......</span></span><br><span class="line">            <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    service_run()</span><br></pre></td></tr></table></figure>
<h2 id="javaandroid开发-udp-客户端"><a class="markdownIt-Anchor" href="#javaandroid开发-udp-客户端"></a> java(Android开发) UDP 客户端</h2>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">uploadUDP</span><span class="params">(String ip, String port)</span>&#123;</span><br><span class="line">    <span class="keyword">new</span> <span class="title class_">Thread</span>() &#123;</span><br><span class="line">        <span class="comment">//线程运行的代码</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.prepare();</span><br><span class="line">                </span><br><span class="line">                <span class="type">InetAddress</span> <span class="variable">address</span> <span class="operator">=</span> InetAddress.getByName(ip);</span><br><span class="line">                <span class="type">DatagramSocket</span> <span class="variable">socket</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramSocket</span>();</span><br><span class="line"></span><br><span class="line">                <span class="type">String</span> <span class="variable">data</span> <span class="operator">=</span> <span class="string">&quot;xxxxxxx&quot;</span>;	</span><br><span class="line">		</span><br><span class="line">                <span class="comment">// 发消息</span></span><br><span class="line">                <span class="type">byte</span>[] data1 = data.getBytes();</span><br><span class="line">                <span class="type">DatagramPacket</span> <span class="variable">packet</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramPacket</span>(data1, data1.length, address, Integer.parseInt(port));</span><br><span class="line">                socket.send(packet);</span><br><span class="line"></span><br><span class="line">                <span class="comment">//收消息</span></span><br><span class="line">                <span class="type">byte</span>[] data2 = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1</span> &lt;&lt; <span class="number">5</span>];</span><br><span class="line">                <span class="type">DatagramPacket</span> <span class="variable">packet2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramPacket</span>(data2, data2.length);</span><br><span class="line">                socket.receive(packet2);</span><br><span class="line">                </span><br><span class="line">                <span class="type">String</span> <span class="variable">reply</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(data2, <span class="number">0</span>, packet2.getLength());</span><br><span class="line">                </span><br><span class="line">                Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;receive : &quot;</span>+reply, Toast.LENGTH_SHORT).show();</span><br><span class="line"></span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.loop();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;.start();<span class="comment">//启动线程</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="javaandroid开发-tcp-客户端"><a class="markdownIt-Anchor" href="#javaandroid开发-tcp-客户端"></a> java(Android开发) TCP 客户端</h2>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">upload</span><span class="params">(String ip, String port)</span>&#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>() &#123;</span><br><span class="line">            <span class="comment">//线程运行的代码</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                <span class="type">boolean</span> <span class="variable">know_error</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.prepare();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">					<span class="comment">// ......</span></span><br><span class="line">                    </span><br><span class="line">                    InetAddress serverip= InetAddress.getByName(ip);;<span class="comment">//定义保存服务器地址的对象</span></span><br><span class="line">                    Socket client=<span class="keyword">new</span> <span class="title class_">Socket</span>(serverip, Integer.parseInt(port));<span class="comment">//定义创建客户端对象的Socket</span></span><br><span class="line">                    </span><br><span class="line">                    OutputStream socketOut=client.getOutputStream(); <span class="comment">//定义发送信息的输出流对象</span></span><br><span class="line">                    InputStream socketIn=client.getInputStream(); <span class="comment">//定义接收数据的输入流</span></span><br><span class="line"></span><br><span class="line">                    <span class="type">byte</span> receive[] = <span class="keyword">new</span> <span class="title class_">byte</span>[buff_size]; <span class="comment">//定义保存客户端发送来的数据的字节数组</span></span><br><span class="line"></span><br><span class="line">                    <span class="comment">// ......</span></span><br><span class="line">                    </span><br><span class="line">                    <span class="comment">//发消息</span></span><br><span class="line">                    <span class="type">String</span> <span class="variable">firstData</span> <span class="operator">=</span> <span class="string">&quot;xxxxxxx&quot;</span>;</span><br><span class="line">                    socketOut.write(firstData.getBytes(<span class="string">&quot;utf-8&quot;</span>));</span><br><span class="line">                    </span><br><span class="line">                    <span class="comment">//接收数据保存在字节数组中, 然后转String字符串</span></span><br><span class="line">                    <span class="type">int</span> len=socketIn.read(receive);</span><br><span class="line">                    String rev=<span class="keyword">new</span> <span class="title class_">String</span>(receive,<span class="number">0</span>,len);</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">if</span> (! rev.equals(<span class="string">&quot;ok&quot;</span>))&#123;</span><br><span class="line">                        know_error = <span class="literal">true</span>;</span><br><span class="line">                        Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;拒绝访问&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Exception</span>(<span class="string">&quot;access deny&quot;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">for</span>(<span class="type">int</span> row=<span class="number">0</span>; row&lt;rowSize ; row++) &#123;</span><br><span class="line"></span><br><span class="line">                        <span class="type">String</span> <span class="variable">data</span> <span class="operator">=</span> Integer.toString(row);</span><br><span class="line"></span><br><span class="line">                        <span class="comment">// 发消息</span></span><br><span class="line">                        socketOut.write(data.getBytes(<span class="string">&quot;utf-8&quot;</span>));</span><br><span class="line"></span><br><span class="line">                        <span class="comment">//接收数据保存在字节数组中, 然后转String字符串</span></span><br><span class="line">                        len=socketIn.read(receive);</span><br><span class="line">                        rev=<span class="keyword">new</span> <span class="title class_">String</span>(receive,<span class="number">0</span>,len);</span><br><span class="line">                        </span><br><span class="line">                        <span class="keyword">if</span> (! rev.equals(<span class="string">&quot;ok&quot;</span>))&#123;</span><br><span class="line">                            know_error = <span class="literal">true</span>;</span><br><span class="line">                            Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;传输中遇到错误&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Exception</span>(<span class="string">&quot;error at half road&quot;</span>);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                    &#125;</span><br><span class="line">                    socketOut.close();</span><br><span class="line">                    socketIn.close();</span><br><span class="line">                    client.close();</span><br><span class="line"></span><br><span class="line">                    Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;同步完成&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                &#125;<span class="keyword">catch</span>(ConnectException e)&#123;</span><br><span class="line">                    Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;未能成功连接服务器&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line">                    Log.i(<span class="string">&quot;tcp-error&quot;</span>, e.getMessage() + e.getStackTrace() + e.getClass());</span><br><span class="line">                    <span class="keyword">if</span>(! know_error)&#123;</span><br><span class="line">                        Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;发生错误&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">// Toast弹窗必要条件2</span></span><br><span class="line">                Looper.loop();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;.start();<span class="comment">//启动线程</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" data-id="cl49a2qlp000198j3hxz4dzgi" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/socket/" rel="tag">socket</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/06/11/%E5%85%B3%E4%BA%8E-socket-io%E5%AE%9E%E7%8E%B0websocket-%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          关于[socket.io实现websocket]部分功能的使用
        
      </div>
    </a>
  
  
    <a href="/2022/06/09/Git%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93-%E4%BB%85%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Git使用总结(仅部分功能)</div>
    </a>
  
</nav>

  
</article>



</section>
        
          <aside id="sidebar">
  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <h1 class="blog-title">蓝湖畔淅淅沥沥的雨</h1>
    <h2 class="blog-subtitle">All tragedy erased, I see only wonders...</h2>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>



    <div class="social-link">
      
        <a class="hvr-bounce-in" href="https://github.com/Wldcmzy" target="_blank" title="Github">
          Github
        </a>
      
        <a class="hvr-bounce-in" href="https://blog.csdn.net/wldcmzy" target="_blank" title="CSDN">
          CSDN
        </a>
      
        <a class="hvr-bounce-in" href="https://space.bilibili.com/83743701" target="_blank" title="bilibili(无技术和学习内容)">
          bilibili(无技术和学习内容)
        </a>
      
    </div>

    <div class="friend-link">
      <h2>友情链接</h2>
      
        <a class="hvr-bounce-in" href="https://shanamaid.github.io/" target="_blank" title="夏娜主题作者的博客">
          夏娜主题作者的博客
        </a>
      
    </div>
  </div>
</div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy;2021 - 2022 StarsWhisper<br>
      由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动 | 
      主题-<a target="_blank" rel="noopener" href="https://github.com/ShanaMaid/hexo-theme-shana">Shana</a>(但是魔改)
      
    </div>
    
  </div>
</footer>
    </div>
    

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/wow/0.1.6/wow.min.js"></script>
<script>
new WOW().init();
</script>   


  
<link rel="stylesheet" href="/plugin/fancybox/jquery.fancybox.css">

  
<script src="/plugin/fancybox/jquery.fancybox.pack.js"></script>




  
<link rel="stylesheet" href="/plugin/galmenu/GalMenu.css">

  
<script src="/plugin/galmenu/GalMenu.js"></script>

  <div class="GalMenu GalDropDown">
      <div class="circle" id="gal">
        <div class="ring">
          
            <a href="/announcement" title="" class="menuItem">公告</a>
          
            <a href="/tags" title="" class="menuItem">标签</a>
          
            <a href="/categories" title="" class="menuItem">分类</a>
          
            <a href="/archives" title="" class="menuItem">归档</a>
          
            <a href="/knightabout" title="" class="menuItem">关于</a>
          
            <a href="/bridges" title="" class="menuItem">传送门</a>
          
        </div>
        
          <audio id="audio" src="#"></audio>
        
      </div> 
</div>
<div id="overlay" style="opacity: 1; cursor: pointer;"></div>
  <script type="text/javascript">var items = document.querySelectorAll('.menuItem');
    for (var i = 0,
    l = items.length; i < l; i++) {
      items[i].style.left = (50 - 35 * Math.cos( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%";
      items[i].style.top = (50 + 35 * Math.sin( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%"
    }</script>
<script type="text/javascript">
  $(document).ready(function() {
    $('body').GalMenu({
      'menu': 'GalDropDown'
    })
  });
</script>

  <section class="hidden-xs"> 
  <ul class="cb-slideshow"> 
    <li><span>苟利</span></li> 
    <li><span>国家</span></li> 
    <li><span>生死以</span></li> 
    <li><span>岂能</span></li> 
    <li><span>祸福</span></li> 
    <li><span>趋避之</span></li> 
  </ul>
</section>

<script src="/js/script.js"></script>




  </div>
</body>
</html>